<?php
// +----------------------------------------------------------------------
 // | Item        管理员模型
 // +----------------------------------------------------------------------
 // | Copyright
 // +----------------------------------------------------------------------
 // | Versions
 // +----------------------------------------------------------------------
 // | Author       YanHao
 // +----------------------------------------------------------------------
 // | Date         2017-07-05
 // +----------------------------------------------------------------------

namespace app\admin\Model;

use traits\model\SoftDelete;
use think\helper\Hash;
use think\Db;

class Admin extends Base
{
    use SoftDelete;

    protected $hidden = ['password'];

    /**
     * [authGroupAccess description]
     * @Author  YanHao
     * @Date    2017-07-10
     * @version [version]
     * @return  [type]     [description]
     */
    public function authGroupAccess()
    {
        return $this->hasMany('AuthGroupAccess', 'uid', 'id')->field('uid,group_id');
    }
    /**
     * 获取某一个管理员的信息
     * @Author  YanHao
     * @Date    2017-07-10
     * @version [version]
     * @param   array      $where [description]
     * @param   boolean    $field [description]
     * @return  [type]            [description]
     */
    public function getOne($id, $field= true)
    {
        $detail = self::with('authGroupAccess')->where('id', $id)->field($field)->find();
        $detail['groups'] = array_map(function($v) {
            return $v['group_id'];
        }, $detail['auth_group_access']);
        return $detail;
    }
    /**
     * 获得全部信息
     * @Author  YanHao
     * @Date    2017-07-11
     * @version [version]
     * @param   array      $where [description]
     * @param   boolean    $field [description]
     * @return  [type]            [description]
     */
    public function getAll(array $where = [], $field = true)
    {
        $info = self::with('authGroupAccess')->where($where)->field($field)->select();
        $authGroup = Db::name('AuthGroup')->column('title', 'id');
        array_walk($info, function(&$v) use($authGroup){
            $v['groups'] = array_map(function($item){
                return $item['group_id'];
            }, $v['auth_group_access']);
            $v['groups_text'] = array_map(function($item) use($authGroup){
                return $authGroup[$item['group_id']];
            }, $v['auth_group_access']);
        });
        return $info;
    }
    /**
     * 登录检查
     * @Author  YanHao
     * @Date    2017-07-05
     * @version [version]
     * @param   array      $post [description]
     * @param   [type]     $ip   [description]
     * @return  [type]           [description]
     */
    public function loginCheck(array $post, $ip)
    {
        $userinfo = $this->visible(['password'])->field(true)->where('user', $post['user'])->find();
        if ($userinfo) {
            if (Hash::check($post['password'], $userinfo['password'])) {
                if ($userinfo['status']) {
                    unset($userinfo['password']);
                    self::_updateLogin($userinfo['id'], $userinfo['login_times'], $ip);
                    $res = ['code' => 1, 'msg' => '登录成功', 'data' => ['info' => $userinfo->toArray()]];
                } else {
                    $res = ['code' => 0, 'msg' => '登录失败', 'data' => ['code' => 0, 'msg' => '账号被禁用']];
                }
            } else {
                $res = ['code' => 0, 'msg' => '登录失败', 'data' => ['code' => 0, 'msg' => '密码不正确']];
            }
        }  else {
            $res = ['code' => 0, 'msg' => '登录失败','data' => ['code' => 0, 'msg' => '账号不存在']];
        }
        return $res;
    }
    /**
     * 更新登陆数据
     * @Author  YanHao
     * @Date    2017-07-05
     * @version [version]
     * @param   [type]     $id    [description]
     * @param   [type]     $times [description]
     * @param   [type]     $ip    [description]
     * @return  [type]            [description]
     */
    private function _updateLogin($id, $times, $ip)
    {
        $updateData = [
            'id'              => $id,
            'last_login_ip'   => $ip,
            'last_login_time' => date('Y-m-d H:i:s'),
            'login_times'     => ++$times,
            'line_status'     => 1
        ];
        return $this->isUpdate(true)->save($updateData);
    }
    /**
     * 更改状态
     * @Author  YanHao
     * @Date    2017-07-05
     * @version [version]
     * @param   [type]     $uid          [description]
     * @param   [type]     $updateStatus [description]
     * @return  [type]                   [description]
     */
    public function changeStatus($uid, $updateStatus)
    {
        $res = $this->isUpdate(true)->save(['line_status' => $updateStatus, 'id' => $uid]);
        return $res ? ['code' => 1, 'msg' => '操作成功'] : ['code' => 0, 'msg' => '操作失败'];
    }
     /**
      * 软删除
      * @Author  YanHao
      * @Date    2017-07-10
      * @version [version]
      * @param   [type]     $id [description]
      */
    public function setDelete($id)
    {
        $this->save(['status'=>0], ['id'=>$id]);
        return self::destroy($id);
    }
    /**
     * 密码加密
     * @Author  YanHao
     * @Date    2017-07-10
     * @version [version]
     */
    public function setPasswordAttr($value)
    {
        return Hash::make($value);
    }
    /**
     * IP转换
     * @Author  YanHao
     * @Date    2017-07-10
     * @version [version]
     * @param   [type]     $value [description]
     * @return  [type]            [description]
     */
    public function getLastLoginIpAttr($value)
    {
        return  long2ip($value);
    }

}